//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.jeecg.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;

public class JxyhPayHttpRequestUtil {
    public JxyhPayHttpRequestUtil() {
    }

    public static Map<String, String> filterBlank(Map<String, String> contentData) throws Exception {
        Map<String, String> submitFromData = new HashMap();
        Set<String> keyset = contentData.keySet();
        Iterator var4 = keyset.iterator();

        while(var4.hasNext()) {
            String key = (String)var4.next();
            String value = (String)contentData.get(key);
            if (StringUtils.isNotBlank(value)) {
                submitFromData.put(key, value.trim());
            }
        }

        return submitFromData;
    }

    public static Map<String, Object> jsonToMap(JSONObject obj) {
        Map<String, Object> data = new HashMap();
        Iterator iterator = obj.entrySet().iterator();

        while(true) {
            while(iterator.hasNext()) {
                Entry<String, Object> entry = (Entry)iterator.next();
                if (entry.getValue() instanceof JSONObject) {
                    data.put((String)entry.getKey(), jsonToMap((JSONObject)entry.getValue()));
                } else if (!(entry.getValue() instanceof JSONArray)) {
                    data.put((String)entry.getKey(), entry.getValue());
                } else {
                    List<Object> list = JSONObject.parseArray(((JSONArray)entry.getValue()).toJSONString());
                    List<Object> newlist = new ArrayList();

                    for(int i = 0; i < list.size(); ++i) {
                        if (list.get(i) instanceof JSONObject) {
                            Map<String, Object> map = jsonToMap((JSONObject)list.get(i));
                            newlist.add(map);
                        } else if (list.get(i) instanceof String) {
                            newlist.add(list.get(i));
                        }
                    }

                    data.put((String)entry.getKey(), newlist);
                }
            }

            return data;
        }
    }

    public static String getParamStr(Map<String, String> params) throws UnsupportedEncodingException {
        StringBuffer content = new StringBuffer();
        if (params == null) {
            return content.toString();
        } else {
            List<String> keys = new ArrayList(params.keySet());
            Collections.sort(keys);

            for(int i = 0; i < keys.size(); ++i) {
                String key = (String)keys.get(i);
                String value = (String)params.get(key);
                if (StringUtils.isNotEmpty(key)) {
                    content.append((i == 0 ? "" : "&") + key + "=" + URLEncoder.encode(value, "UTF-8"));
                }
            }

            return content.toString();
        }
    }

    public static Map<String, String> getParamMap(String paramStr, boolean isUrlEncode, String urlEncodeCharset) throws Exception {
        String[] paramStrs = paramStr.split("&");
        Map<String, String> returnMap = new HashMap();
        String[] var8 = paramStrs;
        int var7 = paramStrs.length;

        for(int var6 = 0; var6 < var7; ++var6) {
            String paramMapStr = var8[var6];
            String paramMapKey = paramMapStr.substring(0, paramMapStr.indexOf("="));
            String paramMapValue = paramMapStr.substring(paramMapStr.indexOf("=") + 1);
            if (isUrlEncode) {
                returnMap.put(paramMapKey, URLEncoder.encode(paramMapValue, urlEncodeCharset));
            } else {
                returnMap.put(paramMapKey, paramMapValue);
            }
        }

        return returnMap;
    }

    public static String sendGet(String url, String param) throws Exception {
        String result = "";
        BufferedReader in = null;
        HttpURLConnection conn = null;

        try {
            Map<String, String> paramMap = getParamMap(param, true, "UTF-8");
            String paramStr = getParamStr(paramMap);
            String urlNameString = url + "?" + paramStr;
            System.out.println("urlNameString=" + urlNameString);
            URL realUrl = new URL(urlNameString);
            conn = (HttpURLConnection)realUrl.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(60000);
            conn.connect();
            int resCode = conn.getResponseCode();
            if (resCode != 200 && resCode != 201 && resCode != 202) {
                in = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
            } else {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            }

            StringBuilder sb = new StringBuilder();

            String line;
            while((line = in.readLine()) != null) {
                sb.append(line);
            }

            result = sb.toString();
        } catch (Exception var20) {
            result = "发送GET请求出现异常！" + var20;
            var20.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                }

                if (in != null) {
                    in.close();
                }
            } catch (Exception var19) {
                var19.printStackTrace();
            }

        }

        if (result.indexOf("发送 POST 请求出现异常！") != -1) {
            throw new Exception(result);
        } else {
            return result;
        }
    }

    public static String sendPost(String url, String param) throws Exception {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";

        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(60000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            int resCode = conn.getResponseCode();
            if (resCode != 200 && resCode != 201 && resCode != 202) {
                in = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
            } else {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            }

            StringBuilder sb = new StringBuilder();

            String line;
            while((line = in.readLine()) != null) {
                sb.append(line);
            }

            result = sb.toString();
        } catch (Exception var18) {
            result = "发送 POST 请求出现异常！" + var18;
            var18.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }

                if (in != null) {
                    in.close();
                }
            } catch (IOException var17) {
                var17.printStackTrace();
            }

        }

        if (result.indexOf("发送 POST 请求出现异常！") != -1) {
            throw new Exception(result);
        } else {
            return result;
        }
    }
}
